package com.unicom.si.controller;

import com.unicom.si.security.pojo.CustomeUserDetail;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
public class LoginController {

    @RequestMapping("welcome")
    public String welcome() {

        return "welcome";
    }

    @RequestMapping("user/{username}")
    public @ResponseBody Object queryUser(@PathVariable("username") String username) {

        Authentication auth =  SecurityContextHolder.getContext().getAuthentication();

        Object obj = auth.getPrincipal();
        if(obj instanceof CustomeUserDetail) {
            CustomeUserDetail customeUser = (CustomeUserDetail)obj;
            System.out.println("email = " + customeUser.getEmail());
            return customeUser;
        }

        return auth;
    }

    @RequestMapping("admin/{num}")
    public @ResponseBody Map<String, String> qyueryAdmin(@PathVariable("num") String num) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("user", "admin");

        return map;
    }
}
